<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */
    
    /*********************************************************
    *            --== InArch ==--
    *
    * Ajax Add Page to Node
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 24.05.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/

    /***
    * class ajaxAddPage
    ****
    * This class will create a new page via an AJAX method
    ****
    * @package ajax
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 24.05.2005
    * @copyright essentialmind.com 2005
    ***/
    class ajaxAddPage extends ajaxAction{

        public function process(){
            //retrieve data
            $title  = $_REQUEST['title'];
            $type   = (int)$_REQUEST['ptype'];
            $parent = (int)$_REQUEST['parent'];
            $orderPos = (int)$_REQUEST['porderPos'];
            
            //needs thought, base 64?
            //$title  = htmlspecialchars($title); //escape variuos shit
            $title  = $this->unescape($title); //title of the new node
            
            //try to add the new node
            try{
                //get the codeset pattern for the current action
                $codeSet        = 'adminNode'.ucfirst($this->db->getOne("SELECT codeset FROM datatype WHERE folder = FALSE AND id = ".$type));
                $addController  = new $codeSet($this->db,$this->smarty,$this->log);                
                $newObjectID    = $addController->addInstance();             

                //do pagemap addition
                $myPageInsert = new pageInserter($this->db, $this->log);
                $myPageInsert->addPageMap($parent,$newObjectID,$type,$title);
                
                //new page id
                $newPageID = $this->db->getOne("SELECT last_value FROM pagemap_id_seq");
                
                // set order position based on ina.ini config: enable_order_position
                if (!($orderPos == null) && $GLOBALS['_CONFIG']['enable_order_position']) {
                    $this->db->Execute("UPDATE pagemap SET order_pos = ". $orderPos . " WHERE id = ". $newPageID);
                }
                
                //log action
                //$this->log->recordAction("Ajax: Successfully added new page to parent node \"$parent\"");

            //something went wrong
            }catch(exception $e){
                // log error
                // $this->log->recordError($e->getMessage());

                //do nothing
            }
            
            //do some smarty magic
            $this->smarty->assign(array('id'=>$newPageID,'title'=>$title));
            $this->r->html  = $this->smarty->preFetch('ajax'.DIRECTORY_SEPARATOR.'newPage.html');
            $this->r->newID = $newPageID;
        }
    }
?>